package com.orhon.smartcampus.modules.student.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.GradesDao;
import com.orhon.smartcampus.modules.base.dao.PeriodsDao;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.entity.Grades;
import com.orhon.smartcampus.modules.base.entity.Periods;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.student.dao.ArrivesDao;
import com.orhon.smartcampus.modules.student.entity.Arrives;
import com.orhon.smartcampus.modules.student.gqlInputModel.GQLArrivesInput;
import com.orhon.smartcampus.modules.student.inputMapper.ArrivesInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddArrivesMutation implements DataFetcher {



    @Autowired
    ArrivesDao arrivesDao;


    @Autowired
    PeriodsDao periodsDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    @Autowired
    GradesDao gradesDao;

    /**
     * 新增/修改届信息
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLArrivesInput input = ArrivesInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));
        Arrives arrives = null;
        if (input.getId() != null){
            arrives = arrivesDao.get(Arrives.class , input.getId());
            arrives.setUpdatedAt(new Date());
        }
        else{
            arrives = new Arrives();
            arrives.setCreatedAt(new Date());
        }
        if(input.getOrder() != null){

            arrives.setArrivesOrder(input.getOrder());
        }
        if(input.getBegin_year() != null){

            arrives.setBeginYear(input.getBegin_year());
        }
        if(input.getGraduate_year() != null){

            arrives.setGraduateYear(input.getGraduate_year());
        }
        if(input.getGarduate_status() != null){

            arrives.setGarduateStatus(input.getGarduate_status());
        }
        if(input.getName() != null){

            arrives.setName(JSON.toJSONString(input.getName()));
        }
        if(input.getMark() != null){

            arrives.setMark(input.getMark());
        }

        if (input.getPeriod() != null){
            Periods periods = periodsDao.get(Periods.class , Long.valueOf(input.getPeriod()));
            arrives.setBasePeriodsByPeriodId(periods);
        }

        if (input.getGrade() != null){
            Grades grades = gradesDao.get(Grades.class , Long.valueOf(input.getGrade()));
            arrives.setBaseGradesByGradeId(grades);
        }

        if (schoolId != null){
            Schools schools = schoolsDao.get(Schools.class ,schoolId);
            arrives.setBaseSchoolsBySchoolId(schools);
        }

        try{
            arrivesDao.saveOrUpdate(arrives);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }

        return arrives;
    }
}

